package cn.lll.vuehouse.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.dto.ProviderPageDTO;
import cn.lll.vuehouse.entity.Customer;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lll.vuehouse.entity.Provider;
import cn.lll.vuehouse.service.ProviderService;
import cn.lll.vuehouse.mapper.ProviderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author asus-pc
 * @description 针对表【bus_provider】的数据库操作Service实现
 * @createDate 2023-09-01 11:34:05
 */
@Service
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider>
        implements ProviderService {

    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public ResponseResult getProviderPage(ProviderPageDTO providerPageDTO) {
        ResponseResult responseResult = new ResponseResult();
        // 供应商分页查询，搜索
        LambdaQueryWrapper<Provider> lambda = new QueryWrapper<Provider>().lambda();
        lambda.like(StringUtils.isNotBlank(providerPageDTO.getProvidername()), Provider::getProvidername, providerPageDTO.getProvidername())
                .like(StringUtils.isNotBlank(providerPageDTO.getConnectionperson()), Provider::getConnectionperson, providerPageDTO.getConnectionperson())
                .eq(ObjectUtil.isNotEmpty(providerPageDTO.getAvailable()), Provider::getAvailable, providerPageDTO.getAvailable())
                .orderByDesc(Provider::getId);
        Page<Provider> providerPage = new Page<>(providerPageDTO.getCurrentPage(), providerPageDTO.getLimit());
        providerMapper.selectPage(providerPage, lambda);
        List<Provider> records = providerPage.getRecords();
        long total = providerPage.getTotal();
        HashMap<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("currentPageData", records);
        responseResult.success(ResponseStatus.SUCCESS, result);
        return responseResult;
    }

    @Override
    public ResponseResult getProviderById(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        Provider provider = providerMapper.selectById(id);
        responseResult.success(ResponseStatus.SUCCESS, provider);
        return responseResult;
    }

    @Override
    public ResponseResult updateUser(Provider provider) {
        ResponseResult responseResult = new ResponseResult();
        providerMapper.updateById(provider);
        responseResult.success();
        return responseResult;
    }

    @Override
    public ResponseResult addUser(Provider provider) {
        ResponseResult responseResult = new ResponseResult();
        // 判断客户是否存在
        String providername = provider.getProvidername();
        LambdaQueryWrapper<Provider> lambda = new QueryWrapper<Provider>().lambda();
        lambda.eq(Provider::getProvidername,providername);
        Provider provider1 = providerMapper.selectOne(lambda);
        if (ObjectUtil.isNotNull(provider1)){
            responseResult.fail(ResponseStatus.PROVIDER_EXIST);
            return responseResult;
        }
        providerMapper.insert(provider);
        responseResult.success();
        return responseResult;
    }
}




